package um.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;


public class LoginActivity extends Activity{
	EditText dni,legajo,passwd;
	Button validar;
	CheckBox checkBox;
	BroadcastReceiver SServiceReciever;

	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        setContentView(R.layout.main);
        

       
        validar = (Button) findViewById(R.id.validar);
        checkBox= (CheckBox) findViewById(R.id.checkBox1);
   
        dni = (EditText) findViewById(R.id.dni);     
        legajo = (EditText) findViewById(R.id.legajo);
        passwd = (EditText) findViewById(R.id.passwd);
 
    	final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

    	
    	IntentFilter filter = new IntentFilter();
        filter.addAction("um.android.data");
        
        final SharedPreferences config = getSharedPreferences("login", MODE_PRIVATE);
        checkBox.setChecked(config.getBoolean("checked", false));
        if(config.getBoolean("checked", false)) {
        	dni.setText(config.getString("dni", ""));
        	legajo.setText(config.getString("legajo", ""));
        	passwd.setText(config.getString("passwd", ""));

        }
        
        
       
        
        SServiceReciever = new BroadcastReceiver() {

    	    @Override
    	    public void onReceive(Context context, Intent intent) {
        	  // TODO Auto-generated method stub
        	  
        	  int SSdata = intent.getIntExtra("retornologin",0);
        	  
        	  if(SSdata != -1) {
        		  Intent optionsAct = new Intent(LoginActivity.this, OptionsActivity.class);
                  startActivity(optionsAct);
        	  }
        	 
        	  else {
        		  alertDialog.setMessage("Verifique los datos ingresados!");
             	 alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
             	    public void onClick(DialogInterface dialog, int which) {
             	       alertDialog.dismiss();
             	    }
             	 });
             	 alertDialog.show(); 
        	  }
    	    
    	    }
        	 
        	};
        	
        	registerReceiver(SServiceReciever, filter);
        	
    	
        validar.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View v)
            { 
            	SharedPreferences.Editor editor = config.edit();
            	if (checkBox.isChecked()) {
            		
            		editor.putBoolean("checked",true);
            		editor.putString("dni", dni.getText().toString());
            		editor.putString("legajo", legajo.getText().toString());
            		editor.putString("passwd", passwd.getText().toString());
            		editor.commit();
                }
            	else {
            		editor.putBoolean("checked",false);
            		editor.commit();
            	}
            	//String datos=newString(passwd.getText()+"/"+legajo.getText()+"/"+passwd.getText());
            	 
                 if(dni.getText().length() != 0 && legajo.getText().length() != 0 && passwd.getText().length() != 0)
                 {
                 Intent i = new Intent (getApplicationContext(), SessionService.class);
            	 i.putExtra("datos", dni.getText()+"/"+legajo.getText()+"/"+passwd.getText());
            	 Log.d("activity", "iniciando service");
            	 v.getContext().startService(i);
                 }
                 else
                 {
                	 
                	 //alertDialog.setTitle("Re");
                	 alertDialog.setMessage("Datos faltantes!");
                	 alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                	    public void onClick(DialogInterface dialog, int which) {
                	       alertDialog.dismiss();
                	    }
                	 });
                	 alertDialog.show(); 
                 }
              
            }
        });
        
        
        
        
    }
    @Override
	public void onDestroy() {
    	super.onDestroy();
		unregisterReceiver(SServiceReciever);
	}
}